package com.example.testtransition

import android.app.ActivityOptions
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.transition.TransitionManager
import android.util.Log
import android.util.Pair
import android.view.View
import android.view.Window
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.testtransition.MyAdapter.Companion.ALBUMS
import com.example.testtransition.databinding.ActivityMainBinding
import com.example.ycshareelement.YcShareElement
import com.example.ycshareelement.transition.CornerImageViewStateSaver
import com.example.ycshareelement.transition.IShareElements
import com.example.ycshareelement.transition.ShareElementInfo
import com.example.ycshareelement.transition.TextViewStateSaver
import com.google.android.material.shape.RelativeCornerSize
import com.google.android.material.transition.platform.MaterialContainerTransformSharedElementCallback
import com.google.android.material.transition.platform.MaterialSharedAxis
import java.util.*

class MainActivity : AppCompatActivity() {

    var binding : ActivityMainBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)
        window.requestFeature(Window.FEATURE_CONTENT_TRANSITIONS)

        setExitSharedElementCallback(MaterialContainerTransformSharedElementCallback())
        window.sharedElementsUseOverlay = false

        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)

        binding?.fabPublish?.showEffect("moment_publish.svga", null)
        binding?.fabPublish?.setmLoops(-1)
        binding?.ivPublishGuide?.setOnClickListener {
            val intent = Intent()
            intent.setClass(this, CreateCommentActivity::class.java)
            val options = ActivityOptions.makeSceneTransitionAnimation(
                this, it, "shared_element_end_root"
            )
            startActivity(intent, options.toBundle())
        }

        binding?.recyclerview?.layoutManager = LinearLayoutManager(applicationContext)
        binding?.recyclerview?.addItemDecoration(DividerItemDecoration(applicationContext, DividerItemDecoration.VERTICAL))

        val sharedAxis = MaterialSharedAxis(MaterialSharedAxis.Z, true)
        sharedAxis.addTarget(binding?.recyclerview?.transitionName)
        TransitionManager.beginDelayedTransition(binding?.recyclerview, sharedAxis)

        val adapter = MyAdapter(object : MyAdapter.AlbumAdapterListener{
            override fun onAlbumClicked(view: View?, album: MyAdapter.Album?) {
                val nickname = view?.findViewById<TextView>(R.id.text_nickname)
                val avatar = view?.findViewById<CustomShapableImageView>(R.id.img_avatar)

                if (nickname == null || avatar == null) return

                val a2 = Pair<View,String>(avatar, "AVATAR")
                val a1 = Pair<View, String>(nickname, "NICKNAME")
//                val arrays = arrayOf(a1, a2)
                avatar?.transitionName = "AVATAR"
                nickname?.transitionName = "NICKNAME"
//                val shapeAppearanceModel = avatar?.shapeAppearanceModel
//                Log.d("MainActivity", "shapeAppearanceModel?.topLeftCornerSize = ${(shapeAppearanceModel?.topLeftCornerSize as RelativeCornerSize?)?.relativePercent}")

//                val elens = arrayOf(ShareElementInfo<Parcelable>(avatar, CornerImageViewStateSaver()),
//                    ShareElementInfo<Parcelable>(nickname, TextViewStateSaver()))

//                val options = ActivityOptions.makeSceneTransitionAnimation(this@MainActivity, nickname, "NICKNAME")
//                val options = ActivityOptions.makeSceneTransitionAnimation(this@MainActivity, *arrays)

                val ishare = IShareElements {
                    arrayOf(ShareElementInfo<Parcelable>(avatar, CornerImageViewStateSaver()),
                        ShareElementInfo<Parcelable>(nickname, TextViewStateSaver())) }

                val bundle = YcShareElement.buildOptionsBundle(this@MainActivity,  ishare)

                val intent = Intent()
                intent.setClass(this@MainActivity, MomentDetailActivity::class.java)
                intent.putExtra("album", album)
//                startActivity(intent, options.toBundle())
                startActivity(intent, bundle)
            }
        })
        binding?.recyclerview?.adapter = adapter
        val albums: List<MyAdapter.Album> = ArrayList<MyAdapter.Album>(ALBUMS)
        adapter.submitList(albums)
    }

    override fun onResume() {
        super.onResume()
        binding?.fabPublish?.startAnimation()
    }

    override fun onPause() {
        super.onPause()

        binding?.fabPublish?.stopEffect()
    }


}